/*
 * @Author: panzhiwei 749458928@qq.com
 * @Date: 2024-02-01 15:38:55
 * @LastEditors: panzhiwei 749458928@qq.com
 * @LastEditTime: 2024-02-02 11:19:08
 * @FilePath: /2DDemo/assets/Script/weapon/ArrowSpawner.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Prefab, instantiate, Vec3, RigidBody2D, Vec2 } from 'cc';
import { Arrow } from './Arrow'; // 引入 Arrow 类
const { ccclass, property } = _decorator;

@ccclass('ArrowSpawner')
export class ArrowSpawner extends Component {
    @property({ type: Prefab })
    arrowPrefab: Prefab = null; // 箭的预制体

    @property({ type: Prefab })
    poisonPrefab: Prefab = null; // 毒箭的预制体

    @property({ type: Prefab })
    rocketCrossPrefab: Prefab = null; // 火箭弩的预制体

    @property({ type: Prefab })
    smallRocketCrossPrefab: Prefab = null; // 火箭弩的预制体

    // 基础弓箭射击方法
    shoot(direction: Vec3, arrowCount: number = 1, angleDifference: number = 10, attackPower: number = 50, position: Vec3) {
        for (let i = 0; i < arrowCount; i++) {
            const newArrow = instantiate(this.arrowPrefab);
            newArrow.setParent(this.node.parent);
            newArrow.setPosition(position);

            // 配置新箭的属性和行为
            let arrowScript = newArrow.getComponent(Arrow);
            if (arrowScript) {
                arrowScript.attackPower = attackPower;
                // 设置新箭的方向和速度
                // 确保新箭的初始位置与当前箭相同
                newArrow.setPosition(position);

                // 计算每个箭的发射角度
                let angleOffset = (i - Math.floor(arrowCount / 2)) * angleDifference;
                let adjustedDirection = this.rotateDirection(direction, angleOffset);
                let velocity = adjustedDirection.multiplyScalar(arrowScript.shootingSpeed);
                let arrowRigidBody = newArrow.getComponent(RigidBody2D);
                if (arrowRigidBody) {
                    arrowRigidBody.linearVelocity = new Vec2(velocity.x, velocity.y);
                }
            }
        }
    }

    // 火箭弩射击方法
    roketCrossbowShoot(direction: Vec3, arrowCount: number = 1, angleDifference: number = 10, attackPower: number = 50, position: Vec3) {
        for (let i = 0; i < arrowCount; i++) {
            const newArrow = instantiate(this.rocketCrossPrefab);
            newArrow.setParent(this.node.parent);
            newArrow.setPosition(position);

            // 配置新箭的属性和行为
            let arrowScript = newArrow.getComponent(Arrow);
            if (arrowScript) {
                arrowScript.attackPower = attackPower;
                // 设置新箭的方向和速度
                // 确保新箭的初始位置与当前箭相同
                newArrow.setPosition(position);

                // 计算每个箭的发射角度
                let angleOffset = (i - Math.floor(arrowCount / 2)) * angleDifference;
                let adjustedDirection = this.rotateDirection(direction, angleOffset);
                let velocity = adjustedDirection.multiplyScalar(arrowScript.shootingSpeed);
                let arrowRigidBody = newArrow.getComponent(RigidBody2D);
                if (arrowRigidBody) {
                    arrowRigidBody.linearVelocity = new Vec2(velocity.x, velocity.y);
                }
            }
        }
    }

    // 生成单个小火箭
    smallRoketCrossbowShoot(position: Vec3, index: number) {
        for (let i = 0; i < index; i++) {
            this.spawnAndShootSmallRocket(position, i,index);
        }
    }

    spawnAndShootSmallRocket(position: Vec3, index: number,arrowCount: number) {
        // 实例化小火箭弩
        // 在方法内部导入 SmallRocket 类
        const smallRocket = instantiate(this.smallRocketCrossPrefab);
        smallRocket.setParent(this.node.parent); // 设置父节点
        smallRocket.setPosition(position); // 设置位置

        // 计算发射方向（示例：根据 index 设置不同的发射角度）
        let angle = index * 120; // 三个火箭弩分别相差120度
        let direction = new Vec3(Math.cos(angle), Math.sin(angle), 0);

        // 设置小火箭弩的属性和发射
        let smallRocketScript = smallRocket.getComponent(Arrow);
        if (smallRocketScript) {
            smallRocketScript.attackPower = smallRocketScript.attackPower / 2; // 设置攻击力
            // smallRocketScript.shoot(direction); // 发射小火箭弩
            // 计算每个箭的发射角度
            let angleDifference = 10;
            let angleOffset = (index - Math.floor(arrowCount / 2)) * angleDifference;
            let adjustedDirection = this.rotateDirection(direction, angleOffset);
            let velocity = adjustedDirection.multiplyScalar(smallRocketScript.shootingSpeed);
            let arrowRigidBody = smallRocket.getComponent(RigidBody2D);
            if (arrowRigidBody) {
                arrowRigidBody.linearVelocity = new Vec2(velocity.x, velocity.y);
            }
        }
    }

    rotateDirection(direction: Vec3, angle: number): Vec3 {
        let radian = angle * Math.PI / 180;
        let rotatedX = direction.x * Math.cos(radian) - direction.y * Math.sin(radian);
        let rotatedY = direction.x * Math.sin(radian) + direction.y * Math.cos(radian);
        return new Vec3(rotatedX, rotatedY, direction.z);
    }

}
